package client;

import common.DataTransferObject;
import common.Email;
import common.LoginPassword;
import common.User;
import server.MyProtocol;
import sun.net.idn.StringPrep;

import javax.swing.*;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.rmi.UnknownHostException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Igor Demidov
 * Date: 16.02.13
 * Time: 20:46
 */
public class MainClient {
    ObjectInputStream in;
    ObjectOutputStream out;
    String currentUserLogin;
    BeginGUI beginGUI;

    MainClient() throws java.net.UnknownHostException {
        Socket socket;
        int serverPort = 8080;
        String address = "localhost";
        InetAddress ipAddress = InetAddress.getByName(address);
        try {
            socket = new Socket(ipAddress, serverPort);
            out = new ObjectOutputStream(socket.getOutputStream());
            in = new ObjectInputStream(socket.getInputStream());
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection");
            System.exit(1);
        }
        beginGUI = new BeginGUI(this);
    }

    private void processCommand(MyProtocol.Command cmd) {
        if (cmd == MyProtocol.Command.OK) {
            JOptionPane.showMessageDialog(beginGUI.getFrame(), "Done", "Done", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(beginGUI.getFrame(), "Error!", "Error!", JOptionPane.ERROR_MESSAGE);
        }
    }

    public boolean doLogin(LoginPassword loginPassword) throws IOException, ClassNotFoundException {
        System.err.println("Try to login: " + loginPassword);
        out.writeObject(new DataTransferObject(MyProtocol.Command.LOGIN, loginPassword));
        DataTransferObject input = (DataTransferObject)in.readObject();
        System.err.println(input.getCmd());
        if (input.getCmd() == MyProtocol.Command.OK) {
            currentUserLogin = loginPassword.getLogin();
            beginGUI.setVisibility(false);
            MessagesPageGUI messagesPageGUI = new MessagesPageGUI(this);
            return true;
        } else {
            processCommand(input.getCmd());
            return false;
        }
    }

    public void doLogout() throws IOException, ClassNotFoundException {
        System.err.println("Try to logout");
        out.writeObject(new DataTransferObject(MyProtocol.Command.LOGOUT));
        DataTransferObject input = (DataTransferObject)in.readObject();
        System.err.println(input.getCmd());
        if (input.getCmd() == MyProtocol.Command.OK) {
            currentUserLogin = null;
            beginGUI.setVisibility(true);
        } else {
            processCommand(input.getCmd());
        }
    }

    public boolean doRegister(User user) throws IOException, ClassNotFoundException {
        System.err.println("Try to register: " + user);
        out.writeObject(new DataTransferObject(MyProtocol.Command.REGISTER, user));
        DataTransferObject input = (DataTransferObject) in.readObject();
        System.err.println(input.getCmd());
        processCommand(input.getCmd());
        return input.getCmd() == MyProtocol.Command.OK;
    }

    public List<Email> getInbox() throws IOException, ClassNotFoundException {
        System.err.println("Try to get inbox");
        out.writeObject(new DataTransferObject(MyProtocol.Command.GET_INBOX, currentUserLogin));
        DataTransferObject input = (DataTransferObject)in.readObject();
        //System.err.println(input.getCmd());
        //System.err.println(((List<Email>) input.getData()).size());
        //System.err.println(((List<Email>) input.getData()).get(0));
        //processCommand(input.getCmd());
        if (input.getData() == null) {
            return null;
        } else {
            return (List<Email>)input.getData();
        }
    }

    public List<Email> getOutbox() throws IOException, ClassNotFoundException {
        System.err.println("Try to get outbox");
        out.writeObject(new DataTransferObject(MyProtocol.Command.GET_OUTBOX, currentUserLogin));
        DataTransferObject input = (DataTransferObject)in.readObject();
        System.err.println(input.getCmd());
        //processCommand(input.getCmd());
        if (input.getData() == null) {
            return null;
        } else {
            return (List<Email>)input.getData();
        }
    }

    public void sendEmail(Email email) throws IOException, ClassNotFoundException {
        System.err.println("Try to send: " + email);
        out.writeObject(new DataTransferObject(MyProtocol.Command.SEND, email));
        DataTransferObject input = (DataTransferObject) in.readObject();
        System.err.println(input.getCmd());
        processCommand(input.getCmd());
    }

    public static void main(String[] args) throws java.net.UnknownHostException {
        new MainClient();
    }
}
